package com.diabetes.service.impl;

import com.diabetes.entity.DeviceData;
import com.diabetes.mapper.DeviceDataMapper;
import com.diabetes.service.DeviceDataService;

import com.jayway.jsonpath.JsonPath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    public void devicePost(Map<String, Object> requestData) {
        // 从请求中获取notify_data
        Map<String, Object> notifyData = (Map<String, Object>) requestData.get("notify_data");

        // 提取所需字段的值
        String eventTime = (String) ((List) JsonPath.read(notifyData, "$..event_time")).get(0);
        String deviceId = (String) ((List) JsonPath.read(notifyData, "$..device_id")).get(0);
        String serviceId = (String) ((List) JsonPath.read(notifyData, "$..service_id")).get(0);
        String bloodSugar = (String) ((List) JsonPath.read(notifyData, "$..blood_sugar")).get(0);

        DeviceData deviceData = new DeviceData();
        deviceData.setNotifyData(notifyData.toString());
        deviceData.setEventTime(eventTime);
        deviceData.setDeviceId(deviceId);
        deviceData.setServiceId(serviceId);
        deviceData.setBloodSugar(bloodSugar);
        deviceDataMapper.insert(deviceData);
    }
}
